package com.css.zfzx.fwsjcj.modules.app.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.zfzx.fwsjcj.modules.app.entity.FwAppVersionEntity;
import com.css.zfzx.fwsjcj.modules.app.entity.FwVersionEntity;
import com.css.zfzx.fwsjcj.modules.app.service.AppManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/fw/app")
public class AppManageController {
    @Autowired
    private AppManageService appManageService;

    /**
     * 查询最新版app
     * @param platform
     * @return
     */
    @GetMapping("getVersion")
    public RestResponse getVersion(@RequestParam("platform") String platform) {
        RestResponse restResponse = null;
        try {
            FwVersionEntity fwVersion=appManageService.getAppVersion(platform);
            restResponse = RestResponse.succeed(fwVersion);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
    /**
     * 保存App版本信息
     * @param fwAppVersionEntity
     * @return
     */
    @PostMapping("/save")
    public RestResponse saveApp(@RequestBody FwAppVersionEntity fwAppVersionEntity){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try {
            appManageService.saveApp(fwAppVersionEntity);
            jsonObject.put("message","保存成功");
            restResponse=RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String messageerror="保存失败";
            restResponse=RestResponse.fail(messageerror);
        }
        return restResponse;
    }

    /**
     * 查询最新版本APP
     * @param type
     * @return
     */
    @GetMapping("/findByType")
    public RestResponse findByType(String type){
        RestResponse restResponse=null;
        try{
            FwAppVersionEntity entity = appManageService.findByType(type);
            restResponse=RestResponse.succeed(entity);
        }catch (Exception e){
            String messsageerror="查询失败";
           restResponse=RestResponse.fail(messsageerror);
        }
        return restResponse;
    }


}
